# -*- coding: utf-8 -*- 
#!/usr/bin/python2.7
"""MVC的cotroler,url映射后会生成对象，并且调用GET或者POST返回"""
from core.decorators import CheckLoginDecorator
from core.files import StaticFile
from core.renderers import MakoRenderer
from core.handlers import RequestHandler
from core.services.FormService import FormServiceFactory
from core.services.DataService import DataServiceFactory
from core.services.OauthService import FlickrOauthService
from core import decorators
from admin.pages import PageFactory
import web

class ReadThemeFile(RequestHandler):
    def GET(self, filePath,other):
        return DataServiceFactory.FileServiceInstance().get('admin/themes/'+filePath)

class DerectToMain(RequestHandler):
    def GET(self):
        web.seeother("/admin/main",absolute=True)

class FlushMemcache(RequestHandler):
    @CheckLoginDecorator
    def GET(self):
        from core.caches import Cache
        Cache.FlushAll()
        web.seeother("/admin/main",absolute=True)

class ViewMainPage(RequestHandler):
    @CheckLoginDecorator
    def GET(self):
        page=PageFactory.MainPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)

class DerectToUpdate(RequestHandler):
    def GET(self):
        web.seeother(web.ctx.path+"/update",absolute=False)

class ViewBlogUpdatePage(RequestHandler):
    def GET(self):
        page=PageFactory.BlogUpdatePageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)

class UpdateBlog(RequestHandler):
    def POST(self):
        formService=FormServiceFactory.BlogUpdateFormServiceInstance()
        if formService.validate():
            formService.submit()
            web.seeother("/admin/blog/update",absolute=True)
        else:
            page=PageFactory.BlogUpdatePageInstance()
            page.form=formService.get()
            renderer=MakoRenderer()
            return renderer.render(page.getTemplateAbsPath(), page)

class Logout(RequestHandler):
    def GET(self):
        DataServiceFactory.AdminServiceInstance().logout()
        web.seeother("/",absolute=True)

class ViewLoginPage(RequestHandler):
    def GET(self):
        page=PageFactory.LoginPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)

class Login(RequestHandler):
    def POST(self):
        formService=FormServiceFactory.LoginFormServiceInstance()
        if formService.validate():
            formService.submit()
        web.seeother("/admin",absolute=True)

class ViewAdminUpdatePage(RequestHandler):
    def GET(self):
        page=PageFactory.AdminEditPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)
class UpdateAdmin(RequestHandler):
    def POST(self):
        formService=FormServiceFactory.AdminUpdateFormServiceInstance()
        if formService.validate():
            formService.submit()
            web.seeother("/admin/admin",absolute=True)
        else:
            page=PageFactory.AdminEditPageInstance()
            page.form=formService.get()
            renderer=MakoRenderer()
            return renderer.render(page.getTemplateAbsPath(), page)
class DerectToList(RequestHandler):
    def GET(self):
        web.seeother(web.ctx.path+"/list",absolute=False)

class ViewArticleListPage(RequestHandler):
    def GET(self):
        page=PageFactory.ArticleListPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)
class ViewArticleNewPage(RequestHandler):
    def GET(self):
        page=PageFactory.ArticleNewPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)
class NewArticle(RequestHandler):
    def POST(self):
        formService=FormServiceFactory.ArticleNewFormServiceInstance()
        if formService.validate():
            formService.submit()
            web.seeother("/admin/article",absolute=True)
        else:
            page=PageFactory.ArticleNewPageInstance()
            page.form=formService.get()
            renderer=MakoRenderer()
            return renderer.render(page.getTemplateAbsPath(), page)

class ViewArticleEditPage(RequestHandler):
    def GET(self,articleID):
        articleID=int(articleID)
        page=PageFactory.ArticleEditPageInstance(articleID)
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)

class UpdateArticle(RequestHandler):
    def POST(self,articleID):
        articleID=int(articleID)
        formService=FormServiceFactory.ArticleUpdateFormServiceInstance(articleID)
        if formService.validate():
            formService.submit()
            web.seeother("/admin/article",absolute=True)
        else:
            page=PageFactory.ArticleEditPageInstance(articleID)
            page.form=formService.get()
            renderer=MakoRenderer()
            return renderer.render(page.getTemplateAbsPath(), page)
class DeleteArticle(RequestHandler):
    def GET(self):
        pass
    

class ViewCategoryListPage(RequestHandler):
    def GET(self):
        page=PageFactory.CategoryListPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)
class ViewCategoryNewPage(RequestHandler):
    def GET(self):
        page=PageFactory.CategoryNewPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)
    
class NewCategory(RequestHandler):
    def POST(self):
        formService=FormServiceFactory.CategoryNewFormServiceInstance()
        if formService.validate():
            formService.submit()
            web.seeother("/admin/category",absolute=True)
        else:
            page=PageFactory.CategoryNewPageInstance()
            page.form=formService.get()
            renderer=MakoRenderer()
            return renderer.render(page.getTemplateAbsPath(), page)

class ViewCategoryEditPage(RequestHandler):
    def GET(self,categoryID):
        categoryID=int(categoryID)
        page=PageFactory.CategoryEditPageInstance(categoryID)
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)

class UpdateCategory(RequestHandler):
    def POST(self,categoryID):
        categoryID=int(categoryID)
        formService=FormServiceFactory.CategoryUpdateFormServiceInstance(categoryID)
        if formService.validate():
            formService.submit()
            web.seeother("/admin/category",absolute=True)
        else:
            page=PageFactory.CategoryEditPageInstance(categoryID)
            page.form=formService.get()
            renderer=MakoRenderer()
            return renderer.render(page.getTemplateAbsPath(), page)

class DeleteCategory(RequestHandler):
    def GET(self):
        pass
class ViewNavigationListPage(RequestHandler):
    def GET(self):
        page=PageFactory.NavigationListPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)
    
class ViewNavigationNewPage(RequestHandler):
    def GET(self):
        page=PageFactory.NavigationNewPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)
    
class NewNavigation(RequestHandler):
    def POST(self):
        formService=FormServiceFactory.NavigationNewFormServiceInstance()
        if formService.validate():
            formService.submit()
            web.seeother("/admin/navigation",absolute=True)
        else:
            page=PageFactory.NavigationNewPageInstance()
            page.form=formService.get()
            renderer=MakoRenderer()
            return renderer.render(page.getTemplateAbsPath(), page)

class ViewNavigationEditPage(RequestHandler):
    def GET(self,navigationID):
        navigationID=int(navigationID)
        page=PageFactory.NavigationEditPageInstance(navigationID)
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)

class UpdateNavigation(RequestHandler):
    def POST(self,navigationID):
        navigationID=int(navigationID)
        formService=FormServiceFactory.NavigationUpdateFormServiceInstance(navigationID)
        if formService.validate():
            formService.submit()
            web.seeother("/admin/navigation",absolute=True)
        else:
            page=PageFactory.NavigationEditPageInstance(navigationID)
            page.form=formService.get()
            renderer=MakoRenderer()
            return renderer.render(page.getTemplateAbsPath(), page)

class DeleteNavigation(RequestHandler):
    def GET(self,navigationID):
        navigationID=int(navigationID)
        DataServiceFactory.NavigationServiceInstance().delete(navigationID)
        web.seeother("/admin/navigation",absolute=True)

class ViewImageListPage(RequestHandler):
    @decorators.CheckOAuthDecorator
    def GET(self):
        page=PageFactory.ImageListPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)

class ViewImageUploadPage(RequestHandler):
    @decorators.CheckOAuthDecorator
    def GET(self):
        page=PageFactory.ImageUploadPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)

class UploadImage(RequestHandler):
    @decorators.CheckOAuthDecorator
    def POST(self):
        formService=FormServiceFactory.ImageUploadFormServiceInstance()
        if formService.validate():
            formService.submit()
            web.seeother("/admin/image/list",absolute=True)
        else:
            web.seeother("/admin/image/upload",absolute=True)
class DeleteImage(RequestHandler):
    def GET(self,photoID):
        if DataServiceFactory.ImageServiceInstance().delete(photoID):
            web.seeother("/admin/image/list",absolute=True)
        else:
            web.seeother("/admin/image/list",absolute=True)

class ViewOauthLinkPage(RequestHandler):
    def GET(self):
        page=PageFactory.ImageOauthLinkPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)

class OAuthorize(RequestHandler):
    def GET(self):
        oauthVerifier=web.input().oauth_verifier
        FlickrOauthService.AuthWithFrob(oauthVerifier)
        #提示成功授权信息～然后关闭
        page=PageFactory.ImageOAuthorizeSuccessPageInstance()
        renderer=MakoRenderer()
        return renderer.render(page.getTemplateAbsPath(), page)

class CancleOAuth(RequestHandler):
    def GET(self):
        FlickrOauthService.CancelAuth()
        web.seeother("/admin/image/upload",absolute=True)
    